package com.yx.study.leet.questionList.questions;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 6. N 字形变换
 * https://leetcode.cn/problems/zigzag-conversion/
 */
public class Convert06 {
    public String convert(String s, int numRows) {
        if (numRows < 2) return s;
        List<StringBuilder> rows = new ArrayList<StringBuilder>();
        for (int i = 0; i < numRows; i++) rows.add(new StringBuilder());
        int i = 0, flag = -1;
        // 每行一个字符串，遍历实现把字符串追加到对应行
        for (char c : s.toCharArray()) {
            rows.get(i).append(c);
            if (i == 0 || i == numRows - 1) flag = -flag;
            i += flag;
        }
        StringBuilder res = new StringBuilder();
        for (StringBuilder row : rows) res.append(row);
        return res.toString();
    }

    @Test
    public void test() {
        // 预期值 "PAHNAPLSIIGYIR"
        System.out.println(convert("PAYPALISHIRING", 3));
    }
}
